// 页面跳转公共方法 便于view层直接使用

import { PageTypes, ProductTypes, ScenicBusinessTypes, TAB_BARS } from "@/constants/common"

// 获取当前页面的层级
export function getPageLevels() {
	return getCurrentPages().length
}
// 页面返回-一般用于顶部返回按钮
export function pageBack() {
	if (getPageLevels() > 1) {
		// 由其他页面进入的直接返回
		uni.navigateBack({
			delta: 1,
		})
	} else {
		// 直接进入的本页面则回首页
		uni.switchTab({
			url: `/pages/home/index`,
		})
	}
}

// 页面跳转
export function navigateTo(url: string, params?: any) {
	if (!url) return

	let paramArr = []
	for (let key in params) {
		paramArr.push(`${key}=${params[key]}`)
	}

	if (TAB_BARS.some((path) => url.includes(path))) {
		uni.switchTab({
			url: url,
		})
	} else {
		uni.navigateTo({ url: `${url}?${paramArr.join("&")}` })
	}
}

/*
link_type: 60
config_data: {jump_url: "/pages/ticket/index", jump_product_type: 0, jump_target_id: "", jump_target_name: ""}
*/
// 根据后台配置的类型进行页面跳转
export function jumpLinkTo(link = {} as any) {
	let { link_type, config_data = {} } = link
	console.log("link", link_type, config_data)
	let jumpUrl = config_data.jump_url || ""
	switch (+link_type) {
		// 不跳转0
		case 0:
			break
		// 跳转产品10
		case PageTypes.PRODUCT:
			switch (config_data.jump_product_type) {
				// 不跳转
				case 0:
					jumpUrl = ""
					break
				case ProductTypes.TICKET:
					// 门票详情
					pageToTicketDetail(config_data.jump_target_id)
					break
				case ProductTypes.COMBO_TKT:
					// 联票详情
					pageToCombotktDetail(config_data.jump_target_id)
					break
				case ProductTypes.TRIP:
					jumpUrl = `/pages/product/trip/index?id=${config_data.jump_target_id}`
					navigateTo(jumpUrl)
					break
			}
			break
		// 跳转景区20
		case PageTypes.SCENIC:
			pageToScenicDetail(config_data.jump_target_id)
			break
		// 跳转H530
		case PageTypes.H5:
			navigateTo("/pages/webview/index", {
				title: config_data.jump_target_name,
				url: encodeURIComponent(jumpUrl),
			})
			break
		// 跳转文章40
		case PageTypes.ARTICLE:
			navigateTo(jumpUrl)
			break
		// 跳转小程序50
		case PageTypes.MINI:
			navigateTo(jumpUrl)
			break
		// 跳转外部小程序60
		case PageTypes.EXT_MINI:
			break
		default:
			break
	}
}

// 区域列表页面
export function pageToDistrict() {
	uni.navigateTo({
		url: `/pages/hy/district/index`,
	})
}

// 景区列表
export function pageToScenicList(params: {}) {
	navigateTo("/pages/hy/scenic/list", params)
}
// 景区详情
export function pageToScenicDetail(id: string) {
	uni.navigateTo({
		url: `/pages/hy/scenic/detail?id=${id}`,
	})
}
// 目的地/区域详情
export function pageToDistrictDetail(id: string) {
	uni.navigateTo({
		url: `/pages/hy/district/detail?id=${id}`,
	})
}
// 产品详情
export function pageToTicketDetail(id: string) {
	uni.navigateTo({
		url: `/pages/product/ticket/index?id=${id}`,
	})
}
// 门票产品预定
export function pageToTicketBooking(params: {}) {
	navigateTo("/pages/product/ticket/submit-order", params)
}
// 套票产品预定
export function pageToCombotktDetail(id: string) {
	uni.navigateTo({
		url: `/pages/product/combotkt/index?id=${id}`,
	})
}
export function pageToCombotktBooking(params: {}) {
	navigateTo("/pages/product/combotkt/submit-order", params)
}

// 线路产品预定
export function pageToTripDetail(id: string, params = {}) {
	navigateTo(`/pages/product/trip/index?id=${id}`, params)
}

// 景区口碑榜
export function pageToRankScenic(id: any) {
	uni.navigateTo({
		url: `/pages/feed/rank/index?id=${id}`,
	})
}

// 订单列表
export function pageToOrderList(params: {}) {
	navigateTo("/pages/home/order", params)
}
// 订单详情
export function pageToOrderTicketDetail(id: string) {
	uni.navigateTo({
		url: `/pages/order/detail/ticket?id=${id}`,
	})
}
